<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Challenges.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_Model_DbTable_Challenges extends Engine_Db_Table {
	protected $_rowClass = 'Vote_Model_Challenge';
	
	public function getSelect($params = array()) {
		$select = $this->select ();
		$prefix = $this->getTablePrefix ();
		
		$select->setIntegrityCheck ( false )->from ( $prefix . 'vote_challenges' )->joinLeft ( $prefix . 'vote_categories', $prefix . 'vote_categories.category_id = ' . $prefix . 'vote_challenges.category_id', array ('category_name' ) )->group ( $prefix . 'vote_challenges.challenge_id' );
		
		if (! empty ( $params ['tag_id'] ) || ! empty ( $params ['tag'] )) {
			$tag = ! empty ( $params ['tag_id'] ) ? $params ['tag_id'] : $params ['tag'];
			if (is_numeric ( $tag )) {
				$select->joinInner ( $prefix . 'core_tagmaps', $prefix . 'core_tagmaps.resource_id = ' . $prefix . 'vote_challenges.challenge_id' )->where ( $prefix . "core_tagmaps.resource_type = 'vote_challenge'" )->where ( $prefix . "core_tagmaps.tag_id = {$tag}" );
			} elseif (is_string ( $tag )) {
				$select->joinInner ( $prefix . 'core_tagmaps', $prefix . 'core_tagmaps.resource_id = ' . $prefix . 'vote_challenges.challenge_id' )->joinLeft ( $prefix . 'core_tags', $prefix . 'core_tagmaps.tag_id = ' . $prefix . 'core_tags.tag_id' )->where ( $prefix . "core_tagmaps.resource_type = 'vote_challenge'" )->where ( $prefix . "core_tags.text = ?", $tag );
			}
		}
		
		if (! empty ( $params ['not_participated'] )) {
			$tmpSelect = $this->select ()->setIntegrityCheck ( false )->from ( $prefix . 'vote_challenge', array ('challenge_id' ) )->where ( $prefix . 'vote_participants.user_id = ?', $params ['not_participated'] );
			
			$contestIds = $this->getAdapter ()->fetchCol ( $tmpSelect );
			if (! empty ( $contestIds )) {
				$select->where ( $prefix . 'vote_challenges.challenge_id NOT IN (?)', $contestIds );
			}
		}
		
		if (! empty ( $params ['category'] )) {
			$select->where ( $prefix . 'vote_challenges.category_id = ?', $params ['category'] );
		}
		
		if (! empty ( $params ['has_photo'] ) && $params ['has_photo']) {
			$select->where ( $prefix . 'vote_challenges.photo_id <> 0' );
		}
		
		if (! empty ( $params ['search'] )) {
			$select->where ( $prefix . "vote_challenges.search = {$params['search']}" );
		}
		
		if (! empty ( $params ['show'] ) && $params ['show'] == 2) {
			$uids = Engine_Api::_ ()->kowcore ()->getFriends ( null, false );
			if (! empty ( $uids )) {
				$select->where ( $prefix . "vote_challenges.user_id IN (?)", $uids );
			} else {
				$select->where ( 'false' );
			}
		}
		
		if (isset ( $params ['sort'] )) {
			switch ($params ['sort']) {
				case 'creation_date' :
				case 'newest' :
					$select->order ( $prefix . 'vote_challenges.creation_date DESC' );
					break;
				case 'view_count' :
				case 'popular' :
					$select->order ( $prefix . 'vote_challenges.view_count DESC' );
					break;
				case 'alphabet' :
					$select->order ( $prefix . 'vote_challenges.title ASC' );
					break;
				default :
					$select->order ( $prefix . 'vote_challenges.challenge_id DESC' );
					break;
			}
		}
		
		if (! empty ( $params ['where'] )) {
			$select->where ( $params ['where'] );
		}
		
		if (! empty ( $params ['keyword'] )) {
			$select->where ( $prefix . "vote_challenges.title LIKE '%{$params['keyword']}%' OR " . $prefix . "vote_challenges.description LIKE '%{$params['keyword']}%' OR " . $prefix . "vote_challenges.keywords LIKE '%{$params['keyword']}%' OR " . $prefix . "vote_challenges.slug LIKE '%{$params['keyword']}%'" );
		}
		
		if (! empty ( $params ['group'] )) {
			$select->group ( $params ['group'] );
		}
		
		if (! empty ( $params ['challenge_id'] )) {
			$select->where ( $prefix . "vote_challenges.challenge_id = ?", $params ['challenge_id'] );
		}
		
		if (! empty ( $params ['approved'] )) {
			$select->where ( $prefix . "vote_challenges.approved = ?", $params ['approved'] );
		}
		
		if (! empty ( $params ['period'] )) {
			if ($params ['period'] == 'past') {
				$select->where ( $prefix . 'vote_challenges.endtime < FROM_UNIXTIME(?)', time () );
			} elseif ($params ['period'] == 'upcoming') {
				$time = time ();
				$select->where ( $prefix . 'vote_challenges.starttime > FROM_UNIXTIME(?)', $time );
			} elseif ($params ['period'] == 'active') {
				$time = time ();
				$select->where ( $prefix . 'vote_challenges.endtime > FROM_UNIXTIME(?) AND ' . $prefix . 'vote_challenges.starttime < FROM_UNIXTIME(?)', $time, $time );
			}
		}
		
		if (! empty ( $params ['user_id'] )) {
			$select->where ( $prefix . "vote_challenges.user_id = ?", $params ['user_id'] );
		}
		
		if (! empty ( $params ['featured'] )) {
			$select->where ( $prefix . "vote_challenges.featured = ?", $params ['featured'] );
		}
		
		if (! empty ( $params ['type'] )) {
			$select->where ( $prefix . "vote_challenges.type = ?", $params ['type'] );
		}
		
		return $select;
	}
	
	public function getPaginator($params = array()) {
		$select = $this->getSelect ( $params );
		$paginator = Zend_Paginator::factory ( $select );
		
		if (! empty ( $params ['ipp'] )) {
			$params ['ipp'] = ( int ) $params ['ipp'];
			$paginator->setItemCountPerPage ( $params ['ipp'] );
		}
		
		if (! empty ( $params ['page'] )) {
			$params ['page'] = ( int ) $params ['page'];
			$paginator->setCurrentPageNumber ( $params ['page'] );
		}
		
		return $paginator;
	}

}